package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dao.entity.*;
import com.niiwoo.civet.user.dao.mapper.*;
import com.niiwoo.civet.user.enums.UserDataTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 用户登录账号本地服务
 */
@Service
public class MobileChangeRecordService{

    @Autowired
    UserAccountMapperExt userAccountMapper;
    @Autowired
    UserBasicInfoMapperExt ucbasicInfoMapper;
    @Autowired
    DataStatusMapperExt dataStatusMapper;
    @Autowired
    LoginAccountMappingMapperExt loginAccountMappingMapper;
    @Autowired
    MobileChangeRecordMapperExt mobileChangeRecordMapper;


    /**
     * 同步更新用户手机号
     * @param accountMapping
     * @param account
     * @param basicInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean smsChangeMobileNo(LoginAccountMapping accountMapping, UcAccount account, UserBasicInfo basicInfo, MobileChangeRecord mobileChangeRecord, DataStatus dataStatus){
        DataStatus dataResult = dataStatusMapper.selectByUserIdAndType(dataStatus.getUserId(), UserDataTypeEnum.OPERATOR_AUTHORIZE.getValue());
        if(dataResult != null) {
            Assert.isTrue(dataStatusMapper.updateByUserIdAndType(dataStatus) > 0, "更新运营商状态失败！");
        }

        Assert.isTrue(loginAccountMappingMapper.updateByAccountName(accountMapping) > 0, "更新账号映射表的用户手机失败！");
        Assert.isTrue(userAccountMapper.updateByPrimaryKeySelective(account) > 0, "更新用户账号表的用户手机失败！");
        Assert.isTrue(ucbasicInfoMapper.updateByPrimaryKeySelective(basicInfo) > 0, "更新用户基础信息表的用户手机失败！");
        Assert.isTrue(mobileChangeRecordMapper.insertSelective(mobileChangeRecord) > 0, "插入用户更换手机记录失败！");
        return true;
    }

    /**
     * 根据用户ID查询手机变更记录
     * @param userId
     * @return
     */
    public MobileChangeRecord selectMobileChangeRecord(String userId){

       return mobileChangeRecordMapper.selectMobileChangeRecord(userId);
    }
}
